<?php
/**
 * 管理中心· 订单管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\Page;
use Think\AjaxPage;
use Hoteladmin\Model\OrderModel;
class OrderController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();        
    
        $this->code_type = C('DAOJU_TYPE');
        $this->expr_type = C('EXPRESS_TYPE');
        
        $this->assign('ctype',$this->code_type); //编码类型
        $this->assign('etype',$this->expr_type); //快递公司
        
        $timegap['beg'] = date('Y-m-d',strtotime("-3 month"));//3个月
        $timegap['end'] = date('Y-m-d',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取上商城订单列表的功能
     * @author:  XC跃
     * @date :2018-06-07
     +--------------------------------------------------------------------------
     */
    public function orderList(){
    	$this->display();
    }
    public function ajaxOrderList(){
        //实例化相关表
        $orident = M('order');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        $condition['or_type']='1';
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
        I('pstate') ? $condition['or_pstate'] = I('pstate') : false;
        I('wstate') ? $condition['or_wstate'] = I('wstate') : false;
        if($beg && $end){
            $condition['or_xtime'] = array('between',"$beg,$end");
        }
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $orident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $orident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->
        field('or_id,or_batch,or_ostate,or_wstate,or_pstate,or_pname,or_gprice,or_xmon,or_jmon,or_zmon,or_add,or_expr,or_xtime')->select();
        foreach($list as $key=>$val){
            $list[$key]['addre'] = unserialize($val['or_add']);
            $list[$key]['expre'] = unserialize($val['or_expr']);
        }
        //dump($list);
        //echo $orident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现管理员设为付款的功能
     * @author:  XC跃
     * @date :2018-06-07
     +--------------------------------------------------------------------------
     */
    public function payOrder(){
        $orid = trim(I('id/d'));
        //实例化相关表
        $orident = M('order');
        $acident = M('account');
        //获取订单信息
        $orFind = $orident->where("`or_id`='".$orid."'")->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'订单参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        if($orFind['or_pstate']=='2' || $orFind['or_ostate']=='2'){
            $res= array('status'=>-1,'msg'=>'该订单为无效或已付款订单'); // 返回结果状态
            exit(json_encode($res));
        }
        if(IS_POST){
            //1、修改支付状态 
            $odata['or_pstate']= '2';
            $odata['or_ptime'] = time();
            $odata['or_pname'] = '管理员付款';
            $ores = $orident->where("`or_id`='".$orid."'")->save($odata);
            //2、减少对应商品的库存
            //minus_stock($order['order_id']);
            //3、添加财务明细记录
            $remark = '管理员设为付款';
            $acident->where("`ac_oid`='".$orid."' and `ac_batch`='".$orFind['or_batch']."'")->setField('ac_desc',$remark);
            //4、记录订单操作日志
            logOrder($orFind['or_id'],'1','订单由'.$remark.'，等待平台发货','付款成功');
        }
        if($ores){
            $res= array('status'=>1,'msg'=>'设为付款成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'设为付款失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现管理员确认收货的功能
     * @author:XC跃
     * @date: 2018-06-07
     +--------------------------------------------------------------------------
     */
    public function takeOrder(){
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $usident = M('user');
        $orident = M('order');
        $deident = M('order_details');
        $reident = M('order_remarks');
        $acident = M('account_log');
        //获取订单记录
        $orFind = $orident->where("`or_id`='".$orid."'")->field('or_id,or_uid,or_ostate,or_wstate,or_zmon')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是发货中状态，不可收货
        if('2'!=$orFind['or_wstate'] || '2'==$orFind['or_ostate']){
            $res= array('status'=>-1,'msg'=>'该订单为无效订单或未发货订单'); // 返回结果状态
            exit(json_encode($res));
        }
        //更改订单信息
        $data['or_wstate']= '3';
        $data['or_qtime'] = time();
        $where = "`or_id`='".$orid."' and `or_pstate`=2 and `or_wstate`=2";
        $ore = $orident->where($where)->save($data);
        if($ore){
            //执行会员消费积分释放的功能
            order_shifang($orFind['or_uid'],$orFind['or_zmon']);
            logOrder($orid,'1','管理员确认收货','确认收货');  //记录订单日志
            $res= array('status'=>1,'msg'=>'确认收货成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'确认收货失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现管理员发货功能
     * @author:  XC跃
     * @date :2018-06-07
     +--------------------------------------------------------------------------
     */
    public function sendGoods(){


        $orid = trim(I('id/d'));
        $expr = $this->expr_type;
        //实例化相关表
        $orident = M('order');
        //获取系统参数
        $sms = tpCache("sMes.fahuo");
        //获取订单信息
        $orFind = $orident->where("`or_id`='".$orid."'")->find();

        if(count($orFind)=='0'){
            layer_error('参数错误','Order/orderList');
        }
        if(IS_POST){
            if($orFind['or_pstate']!='2' || $orFind['or_ostate']=='2'){
                $res= array('status'=>-1,'msg'=>'该订单为无效或未付款订单'); // 返回结果状态
                exit(json_encode($res));
            }
            //快递信息序列化
            $express = array(
                'ename' => $expr[trim(I('post.type'))][0],
                'ecode' => $expr[trim(I('post.type'))][1],
                'enumb' => trim(I('post.numb')),
            );
            //更新发货状态
            $data['or_wstate']= '2';
            $data['or_ftime'] = time();
            $data['or_expr']  = serialize($express);
            //发送手机短信
            if('2' == $data['or_wstate'] && '1' == $sms){
                $addre = unserialize($orFind['or_add']);
                $params['ordsn'] = substr($orFind['or_batch'],-6);
                $params['consi'] = $addre['aname'];
                $resp = sendSms('7',$addre['phone'],$params,session_id());
            }
            //echo $orid;
            $ores = $orident->where("`or_id`='".$orid."'")->save($data);
            //记录订单日志
            logOrder($orid,'1','管理员执行订单发货','订单发货');
            if($ores){
                $res= array('status'=>1,'msg'=>'订单发货成功'); // 返回结果状态
            }else{
                $res= array('status'=>-1,'msg'=>'订单发货失败'); // 返回结果状态
            }
            exit(json_encode($res));
        }else{
            //订单未付款发货
            if($orFind['or_pstate']!='2' || $orFind['or_ostate']=='2'){
                layer_error('该订单为无效或未付款订单','Order/orderList');
            }
            $orFind['addre'] = unserialize($orFind['or_add']);
            $this->assign('ores',$orFind);
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取订单详情的功能
     * @author： XC跃
     * @date 2018-06-07
     +--------------------------------------------------------------------------
     */
    public function orderShow(){
        $orid = trim(I('get.id'));
        //实例化相关Model
        $oridentr = new OrderModel();
        $res = $oridentr->orderShow($orid);
        //dump($res);die;
        $this->assign('ores',$res['ores']);
        $this->assign('goList',$res['goList']);
        $this->assign('reList',$res['reList']);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现删除无效订单的功能
     * @author： XC跃
     * @date 2018-06-07
     +--------------------------------------------------------------------------
     */
    public function delOrder(){
        $orid = trim(I('post.id'));
         //实例化相关表
        $orident = M('order');
        $deident = M('order_details');
        $reident = M('order_remarks');
        $acident = M('acc_shop_log');
    
        //获取订单记录
        $orFind = $orident->where("`or_id`='".$orid."'")->field('or_id,or_ostate,or_pstate,or_batch')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是未支付状态，不可删除
        if('1'!=$orFind['or_pstate'] || '2'!=$orFind['or_ostate']){
            $res= array('status'=>-1,'msg'=>'该订单不可删除'); // 返回结果状态
            exit(json_encode($res));
        }
        //删除订单记录
        if($orid>0){
            $orident->where("`or_id`='".$orid."'")->delete(); //删除订单记录
            $deident->where("`de_oid`='".$orid."'")->delete();  //订单详细
            $reident->where("`re_oid`='".$orid."'")->delete();  //订单日志
            $acre = $acident->where("`ac_oid`='".$orFind['or_id']."' and `ac_batch`='".$orFind['or_batch']."'")->delete(); //删除财务记录
        }
        if($acre){
            $res= array('status'=>1,'msg'=>'删除成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'删除失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现订单（配货单）打印的功能
     * @author： XC跃
     * @date 2018-06-07
     +--------------------------------------------------------------------------
     */
    public function orderPrint(){
        $oid = trim(I('get.id'));
        //$type= I('get.type');
        //实例化相关Model
        $oidentr = new OrderModel();
        $res = $oidentr->orderShow($oid);
        $this->assign('ores',$res['ores']);
        $this->assign('goList',$res['goList']);
        $this->assign('reList',$res['reList']);
        $this->assign('logoImgUrl', tpCache("sBas.logoImgUrl"));
        $this->display();
    }
    
    /**
     +--------------------------------------------------------------------------
     * 实现订单快递查询的功能
     * @author:  wjl
     * @date :2018-01-18
     +--------------------------------------------------------------------------
     */
    public function express(){
        $com = I('get.com');
        $nu  = I('get.nu');
        $url = express($com,$nu);
        $this->assign("url",$url);
        $this->display();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现获取发货订单列表的功能
     * @author:  XC跃
     * @date :2018-09-29
    +--------------------------------------------------------------------------
     */
    public function fahuoList(){
        $this->display();
    }
    public function ajaxFahuoList(){
        //实例化相关表
        $orident = M('order');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        $condition['or_type']='1';
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
        I('pstate') ? $condition['or_pstate'] = I('pstate') : false;
        I('wstate') ? $condition['or_wstate'] = I('wstate') : false;
        if($beg && $end){
            $condition['or_xtime'] = array('between',"$beg,$end");
        }
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $orident->where($condition)->where("`or_wstate`=1 and `or_pstate`=2")->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $orident->where("`or_wstate`=1 and `or_pstate`=2")->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->
        field('or_id,or_batch,or_ostate,or_wstate,or_pstate,or_pname,or_gprice,or_xmon,or_jmon,or_zmon,or_add,or_expr,or_xtime')->select();
        foreach($list as $key=>$val){
            $list[$key]['addre'] = unserialize($val['or_add']);
            $list[$key]['expre'] = unserialize($val['or_expr']);
        }
        //dump($list);
        //echo $orident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }

/******************************* 兑换商品订单  ****************************************/
    /**
     +--------------------------------------------------------------------------
     * 实现获取兑订单列表的功能
     * @author:GH
     * @date :2018-12-29
     +--------------------------------------------------------------------------
     */
    public function prorderList(){
        $this->display();
    }
    public function ajaxProrderList(){
        //实例化相关表
        $orident = M('order');
        $grident = M('grade');
        $poident = M('prop_order');
        $prident = M('prop');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        $condition['or_type']='2';
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
        if($beg && $end){
            $condition['or_atime'] = array('between',"$beg,$end");
        }
        $sort_order = I('order_by').' '.I('sort');
        $count = $orident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $poident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach($list as $key=>$val){
//            $list[$key]['grade'] = $grident->where("`gr_id`='".$val['or_goid']."'")->find();
            $list[$key]['prop'] = $prident->where("`gp_id`='".$val['or_goid']."'")->find();
            $list[$key]['add'] = unserialize($val['or_addre']);
        }
        //echo $poident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取兑换订单详情的功能
     * @author： GH
     * @date 2018-12-30
     +--------------------------------------------------------------------------
     */
    public function prorderShow(){
        $orid = trim(I('get.id'));
        //实例化相关Model
        $oridentr = new OrderModel();
        $res = $oridentr->prorderShow($orid);
        $this->assign('ores',$res['ores']);
        $this->assign('goList',$res['goList']);
        $this->assign('reList',$res['reList']);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现管理员兑换订单发货功能
     * @author:  GH
     * @date :2018-12-31
     +--------------------------------------------------------------------------
     */
    public function prsendGoods(){
        $orid = trim(I('id/d'));
        $expr = $this->expr_type;
        //实例化相关表
        $orident = M('prop_order');
        //获取订单信息
        $orFind = $orident->where("`or_id`='".$orid."'")->find();
        if(count($orFind)=='0'){
            layer_error('参数错误','Order/orderList');
        }
        if(IS_POST){
            if($orFind['or_wstate']!='3'){
                $res= array('status'=>-1,'msg'=>'该订单已经发货'); // 返回结果状态
                exit(json_encode($res));
            }
            //快递信息序列化
            $express = array(
              'ename' => $expr[trim(I('post.type'))][0],
              'ecode' => $expr[trim(I('post.type'))][1],
              'enumb' => trim(I('post.numb')),
            );
            //更新发货状态
            $data['or_wstate']= '2';
            $data['or_ftime'] = time();
            $data['or_expre']  = serialize($express);
            //echo $orid;
            $ores = $orident->where("`or_id`='".$orid."'")->save($data);
            if($ores){
                $res= array('status'=>1,'msg'=>'订单发货成功'); // 返回结果状态
            }else{
                $res= array('status'=>-1,'msg'=>'订单发货失败'); // 返回结果状态
            }
            exit(json_encode($res));
        }else{
            //订单未付款发货
            if($orFind['or_wstate']!='3'){
                layer_error('该订单已经发货','Order/prorderList');
            }
            $orFind['addre'] = unserialize($orFind['or_addre']);
            $this->assign('ores',$orFind);
            $this->display();
        }
    }
    /******************************* 代理商品订单  ****************************************/
    /**
    +--------------------------------------------------------------------------
     * 实现获取兑订单列表的功能
     * @author:GH
     * @date :2018-12-29
    +--------------------------------------------------------------------------
     */
    public function dailiList(){
        $this->display();
    }
    public function ajaxDailiList(){
        //实例化相关表
        $orident = M('order');
        $doident = M('daili_order');
        $dgident = M('daili_goods');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
        if($beg && $end){
            $condition['do_atime'] = array('between',"$beg,$end");
        }
        $sort_order = I('order_by').' '.I('sort');
        $count = $doident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $doident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $key=>$val){
            $list[$key]['prop'] = $dgident->where("`dg_id`='".$val['do_goid']."'")->find();
           $list[$key]['add'] = unserialize($val['do_addre']);
       }
        //echo $poident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现获取兑换订单详情的功能
     * @author： GH
     * @date 2018-12-30
    +--------------------------------------------------------------------------
     */
    public function dailiShow(){
        $orid = trim(I('get.id'));
        //实例化相关Model
        $oridentr = new OrderModel();
        $res = $oridentr->prorderShow($orid);
        $this->assign('ores',$res['ores']);
        $this->assign('goList',$res['goList']);
        $this->assign('reList',$res['reList']);
        $this->display();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现管理员兑换订单发货功能
     * @author:  GH
     * @date :2018-12-31
    +--------------------------------------------------------------------------
     */
    public function dailiGoods(){
        $orid = trim(I('id/d'));
        $expr = $this->expr_type;
        //实例化相关表
        $orident = M('prop_order');
        //获取订单信息
        $orFind = $orident->where("`or_id`='".$orid."'")->find();
        if(count($orFind)=='0'){
            layer_error('参数错误','Order/orderList');
        }
        if(IS_POST){
            if($orFind['or_wstate']!='3'){
                $res= array('status'=>-1,'msg'=>'该订单已经发货'); // 返回结果状态
                exit(json_encode($res));
            }
            //快递信息序列化
            $express = array(
              'ename' => $expr[trim(I('post.type'))][0],
              'ecode' => $expr[trim(I('post.type'))][1],
              'enumb' => trim(I('post.numb')),
            );
            //更新发货状态
            $data['or_wstate']= '2';
            $data['or_ftime'] = time();
            $data['or_expre']  = serialize($express);
            //echo $orid;
            $ores = $orident->where("`or_id`='".$orid."'")->save($data);
            if($ores){
                $res= array('status'=>1,'msg'=>'订单发货成功'); // 返回结果状态
            }else{
                $res= array('status'=>-1,'msg'=>'订单发货失败'); // 返回结果状态
            }
            exit(json_encode($res));
        }else{
            //订单未付款发货
            if($orFind['or_wstate']!='3'){
                layer_error('该订单已经发货','Order/prorderList');
            }
            $orFind['addre'] = unserialize($orFind['or_addre']);
            $this->assign('ores',$orFind);
            $this->display();
        }
    }
}